##################################################################################
#                         GOV 2001: REPLICATION PROJECT                          #
#  Replication Article: Lipsmeyer, Christine S. & Zhu, Ling (2011)  -            #
# "IMMIGRATION, GLOBALIZATION, AND UNEMPLOYMENT BENEFITS IN DEVELOPED EU STATES" #
#                       AJPS Vol. 55(3), Pp. 647-664                             #
#              Replication Authors: Diana Draghici & Jerome Hughes               #
#                       File Version: Tue, 24 Apr 2012                           #     
##################################################################################


#################################################################################
#   PERFORM MULTIPLE IMPUTATION ON JOINT DATA SET BASED ON ORIGINAL FILES       #    
#   [Downloaded from the sources indicated by the authors in the article]       #  
#################################################################################


#################################################################################
#       Preliminaries                                                           #                   
#################################################################################


## Set working directory 
setwd("//Users/DianaDraghici//Documents//HARVARD UNIVERSITY//Courses//Spring Semester 2012//GOV 2001//Replication Project//Lipsmeyer&Zhu_AJPS_2011//Additional Data")

## Load libraries
library(foreign)
library(Amelia)

## Import data
Data <- read.table("AJPS2011REPL.csv", sep = ",", header = TRUE)


#################################################################################
#       Generate Multiply Imputed Datasets                                      #                   
#################################################################################
head(Data)
summary(Data )

set.seed(02138)
a.out <- amelia(Data, m=5, ts="year", cs="state", ids=c("state", "year"), 
sqrts=c("Entitlement", "Imm_Inflows", "Imm_Outflows", "UnitLabCost", 
"UD", "LEFTGS", "netden", "UnemplRate"),
p2s=2, empri = 0.01 * nrow(Data))
a.out


### Export multiply imputed datasets
write.amelia(obj=a.out, file.stem="AJPS_MI", separate=FALSE, format="dta")
write.amelia(obj=a.out, file.stem="AJPS_MI", format="dta")


## Change working directory 
setwd("//Users/DianaDraghici//Documents//HARVARD UNIVERSITY//Courses//Spring Semester 2012//GOV 2001//Replication Project//Lipsmeyer&Zhu_AJPS_2011")
##

pdf("aout.pdf", 12, 6)
plot(a.out)
dev.off()

pdf("adisp.pdf")
disperse(a.out, dims = 1, m = 5)
dev.off()


missmap(a.out)



tscsPlot(a.out, var = "Entitlement", cs = 1, main="Austria")
overimpute(a.out, var = "Entitlement", cs=1)
disperse(a.out, dims = 1, m = 5)

############################################################################


## Set working directory 
setwd("//Users/DianaDraghici//Documents//HARVARD UNIVERSITY//Courses//Spring Semester 2012//GOV 2001//Replication Project//Lipsmeyer&Zhu_AJPS_2011//Additional Data")


AJPS <- read.dta("AJPS_MI.dta")
head(AJPS,10)

## Construct Net Immigration Rate (Equation 1 on p.651)
AJPS$netimmrate <- (AJPS$Imm_Inflows - AJPS$Imm_Outflows)/1000

summary(AJPS$netimmrate)

## Construct EU Integration Measure (Equations 2-4 on p.652)

# EU Avg

AJPS$EUAvg <- NA

for (i in 1971:2007) {        # loop thru years
	for (j in 0:5) {             # loop thru imputed datasets
AJPS[which(AJPS[,"year"]==i & AJPS[,"imp"]==j), ]$EUAvg <- mean(AJPS[which(AJPS[,"year"]==i & AJPS[,"imp"]==j), ]$UnitLabCost, na.rm=TRUE)
                           }
         }

# Xit
AJPS$Xit <- abs(AJPS$EUAvg - AJPS$UnitLabCost)

# Mt 
AJPS$Mt <- NA

for (i in 1971:2007) {        # loop thru years
	for (j in 0:5) {             # loop thru imputed datasets
AJPS[which(AJPS[,"year"]==i & AJPS[,"imp"]==j), ]$Mt <- max(AJPS[which(AJPS[,"year"]==i & AJPS[,"imp"]==j), ]$Xit)
                           }
         }

# EU Integration Index
AJPS$laborcost_new <-   1- (AJPS$Xit/ AJPS$Mt)


# Verif
subset(AJPS, select=c("state", "year","UnitLabCost", "EUAvg", "Xit", "Mt", "laborcost_new", "imp"))
######

## New variables to correspond to names in original dataset

AJPS$i_entitlement <- AJPS$Entitlement  
AJPS$leftgov      <- AJPS$LEFTGS 
AJPS$unemp <- AJPS$UnemplRate
AJPS$gdp <- AJPS$cgdp
AJPS$trade <- AJPS$openk
AJPS$fdi <- AJPS$ci
AJPS$i_socialcorp3 <- AJPS$netden

Data <- subset(AJPS, select=c("state", "year", "netimmrate", "leftgov", "unemp",  "trade",
"fdi", "gdp", "laborcost_new", "i_entitlement", "i_socialcorp3", "imp"))
head(Data)

####

# Separate MIDS

data0 <- subset(Data, imp==0)
data1 <- subset(Data, imp==1)
data2 <- subset(Data, imp==2)
data3 <- subset(Data, imp==3)
data4 <- subset(Data, imp==4)
data5 <- subset(Data, imp==5)




